﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanLyKhachHang
{
    public class DanhMucKhachHang : DBInteractionBase
    {
        public string TaiKhoan { get; set; }
        public string MaKhachHang { get; set; }
        public string HoTen { get; set; }
        public DateTime NgaySinh { get; set; }
        public bool GioiTinh { get; set; }
        public string DiaChi { get; set; }
        public string DienThoai { get; set; }
        public string Email { get; set; }
        public DateTime NgayDen { get; set; }
        public string HinhAnh { get; set; }

        public bool Insert(DanhMucKhachHang obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucKhachHang_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@NgaySinh", obj.NgaySinh));
                    dbcmd.Parameters.Add(new SqlParameter("@GioiTinh", obj.GioiTinh));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@DienThoai", obj.DienThoai));
                    dbcmd.Parameters.Add(new SqlParameter("@Email", obj.Email));
                    dbcmd.Parameters.Add(new SqlParameter("@NgayDen", obj.NgayDen));
                    dbcmd.Parameters.Add(new SqlParameter("@HinhAnh", obj.HinhAnh));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DanhMucKhachHang obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucKhachHang_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhachHang", obj.MaKhachHang));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@NgaySinh", obj.NgaySinh));
                    dbcmd.Parameters.Add(new SqlParameter("@GioiTinh", obj.GioiTinh));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@DienThoai", obj.DienThoai));
                    dbcmd.Parameters.Add(new SqlParameter("@Email", obj.Email));
                    dbcmd.Parameters.Add(new SqlParameter("@NgayDen", obj.NgayDen));
                    dbcmd.Parameters.Add(new SqlParameter("@HinhAnh", obj.HinhAnh));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectByMaKhachHang(string makhachhang)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucKhachHang_SelectByMaKhachHang", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhachHang", makhachhang));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucKhachHang_SelectByMaKhachHang: " + ex.Message);
            }
        }

        public DataTable SelectAll(DanhMucKhachHang obj, DateTime tungay, DateTime denngay, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucKhachHang_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TuNgay", tungay));
                    dbcmd.Parameters.Add(new SqlParameter("@DenNgay", denngay));
                    dbcmd.Parameters.Add(new SqlParameter("@MaKhachHang", string.IsNullOrEmpty(obj.MaKhachHang) ? "" : obj.MaKhachHang));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", string.IsNullOrEmpty(obj.HoTen) ? "" : obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucKhachHang_SelectAll: " + ex.Message);
            }
        }
    }
}
